#region using directives 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Messaging;
#endregion

namespace MapiApplication
{
    public partial class frmCreateNew : Form
    {
        #region Globals
        MapiManager objMapiManager = null;
        SendMailOptions options;
        Mail mail = new Mail();
        Credential credential;

        List<string> credentialList = null;

        #endregion

        #region Events
        public frmCreateNew()
        {
        }

        public frmCreateNew(MapiManager mapiManager, ref List<string>  credentialList)
        {
            InitializeComponent();
            objMapiManager = mapiManager;

            this.credentialList = credentialList;
        }

        public frmCreateNew(MapiManager mapiManager, string toNameList, string ccNameList, string subject, string messageBody)
        {
            InitializeComponent();
            objMapiManager = mapiManager;

            txtTo.Text = toNameList;
            txtCc.Text = ccNameList;
            txtSubject.Text = subject;
            txtMessagebody.Text = messageBody;
        }

        private void btnAttachment_Click(object sender, EventArgs e)
        {
            Browse();
        }

        private void toolStripAttachment_Click(object sender, EventArgs e)
        {
            Browse();
        }

        private void toolstripSend_Click(object sender, EventArgs e)
        {
            SendMail();
        }

        private void frmCreateNew_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        #endregion

        #region Helper Methods

        private void Browse()
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "Select File";
            fd.InitialDirectory = @"c:\";
            fd.Filter = "All files (*.*)|*.*";
            fd.FilterIndex = 2;
            fd.RestoreDirectory = true;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (txtAttachment.Text != String.Empty)
                    txtAttachment.Text += ";" + fd.FileName;
                else
                    txtAttachment.Text = fd.FileName;
            }
        }

        private void SendMail()
        {
            try
            {
                List<string> toList = new List<string>();
                List<string> ccList = new List<string>();
                List<string> bccList = new List<string>();
                List<string> attachedFilesList = new List<string>();
                char[] splitter = { ';' };

                string[] toNames;
                toNames = txtTo.Text.Split(splitter);
                foreach (string to in toNames)
                {
                    if (to != String.Empty)
                        toList.Add(to.Trim());
                }

                string[] ccNames;
                ccNames = txtCc.Text.Split(splitter);
                foreach (string cc in ccNames)
                {
                    if (cc != String.Empty)
                        ccList.Add(cc.Trim());
                }

                string[] bccNames;
                bccNames = txtBcc.Text.Split(splitter);
                foreach (string bcc in bccNames)
                {
                    if (bcc != String.Empty)
                        bccList.Add(bcc.Trim());
                }

                string[] attachedFiles;
                attachedFiles = txtAttachment.Text.Split(splitter);
                foreach (string file in attachedFiles)
                {
                    if (file != String.Empty)
                        attachedFilesList.Add(file.Trim());
                }

                if (chkSmtp.Checked)
                {
                    mail.Attachments = attachedFilesList;
                    mail.ToList = toList;
                    mail.CcList = ccList;
                    mail.BccList = bccList;
                    mail.Subject = txtSubject.Text;
                    mail.MessageBody = txtMessagebody.Text;
                    this.objMapiManager.SendMailThroughSMTPServer(mail);
                    this.Close();
                }
                else
                {
                    objMapiManager.SendMail(toList, ccList, bccList, txtSubject.Text, txtMessagebody.Text, attachedFilesList);
                    this.Close();
                }
            }
            catch (MapiManagerException mapiException)
            {
                MessageBox.Show(mapiException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentNullException argNullException)
            {
                MessageBox.Show(argNullException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentException argException)
            {
                MessageBox.Show(argException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException invalidOperation)
            {
                MessageBox.Show(invalidOperation.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (NullReferenceException nullrefernceException)
            {
                MessageBox.Show(nullrefernceException.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        private void chkSmtp_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSmtp.Checked)
            {
                this.toolStripCredential.Visible = true;
                this.toolStripOptions.Visible = true;
            }
            else
            {
                this.toolStripCredential.Visible = false;
                this.toolStripOptions.Visible = false;
            }
        }

        private void toolStripCredential_Click(object sender, EventArgs e)
        {
            credential = new Credential(ref this.objMapiManager,ref credentialList);
            credential.ShowDialog();
            
        }

        private void toolStripOptions_Click(object sender, EventArgs e)
        {
            options = new SendMailOptions(ref this.mail, ref this.objMapiManager);
            options.ShowDialog();
        }
    }
}